package com.xci.platform.auth.controller;

import com.xci.platform.annotation.Authorize;
import com.xci.platform.auth.entity.ContentEntity;
import com.xci.platform.auth.service.ContentService;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.JsonGrid;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 系统内容字典控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/content")
public class ContentController extends AuthBaseController {
    /**
     * 内容字典服务
     */
    @Resource
    protected ContentService contentService;

    //region 页面视图

    /**
     * 内容字典首页
     */
    @GetMapping
    @Authorize(code = "SystemContent")
    public String index() {
        return "auth/content/index";
    }

    /**
     * 内容字典新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemContent.Create")
    public String create(ModelMap map) {
        createMark(map);
        ContentEntity entity = new ContentEntity();
        entity.setId(StringHelper.guid());
        entity.setContentFormat(1);
        map.put("entity", entity);
        return "auth/content/edit";
    }

    /**
     * 内容字典编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "SystemContent.Edit")
    public String edit(String id, ModelMap map) {
        ContentEntity entity = contentService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/content/edit";
    }

    /**
     * 内容字典详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        ContentEntity entity = contentService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/content/details";
    }

    /**
     * 内容字典单独编辑
     */
    @GetMapping("/{code}")
    public String index(@PathVariable String code, ModelMap map) {
        if (StringHelper.isBlank(code)) return notFound();
        ContentEntity entity = contentService.queryByCode(code);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/content/single-edit";
    }
    //endregion

    //region 数据处理

    /**
     * 内容字典表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemContent.Search")
    public JsonGrid grid(@RequestParam Map params) {
        return BoolMessage.grid(contentService.queryPage(params));
    }

    /**
     * 保存新增内容字典
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize(code = "SystemContent.Create")
    public BoolMessage createSave(@ModelAttribute ContentEntity entity) {
        return contentService.save(entity, true);
    }

    /**
     * 保存修改内容字典
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize(code = "SystemContent.Edit")
    public BoolMessage editSave(@ModelAttribute ContentEntity entity) {
        return contentService.save(entity, false);
    }

    /**
     * 保存单个内容
     */
    @ResponseBody
    @PostMapping("/single-save")
    @Authorize(code = "SystemContent.Edit")
    public BoolMessage singleSave(@ModelAttribute ContentEntity entity) {
        ContentEntity origin = contentService.queryById(entity.getId());
        origin.setContentFormat(entity.getContentFormat());
        origin.setContentValue(entity.getContentValue());
        return contentService.save(origin, false);
    }

    /**
     * 删除内容字典
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemContent.Delete")
    public BoolMessage delete(String ids) {
        for (String id : StringHelper.splitToArray(ids)) {
            contentService.delete(id);
        }
        return BoolMessage.True;
    }

    //endregion
}
